let express = require('express');
let router = express.Router();

const userService = require('../service/userService');

/*
 * 用户使用账号密码登录接口
 */
// router.post('/loginByAccountName', async function (req, res, next) {
//     let accountName = req.body.accountName;
//     let password = req.body.password;
//     if (!accountName || !password) {
//         res.json({ msg: "用户名或密码为空", jwt: "" });
//         return ;
//     }
//     let result = await userService.loginByAccountName(accountName, password);
//     res.json(result);
// });

/*
 * 用户使用手机密码登录接口
 */
// router.post('/loginByPhone', async function (req, res, next) {
//     let phone = req.body.phone;
//     let password = req.body.password;
//     if (!phone || !password) {
//         res.status(400).json({ msg: "手机号或密码为空", jwt: "" });
//         return ;
//     }
//     let result = await userService.loginByPhone(phone, password);
//     res.status(result.success ? 200 : 400).json(result);
// });

/*
 * 用户使用工号密码登录接口
 */
router.post('/loginByWorkId', async function (req, res, next) {
    console.log('【请求到达的时间】', Date());
    let workId = req.body.workId;
    let password = req.body.password;
    if (!workId || !password) {
        res.status(400).json({ msg: "工号或密码为空", jwt: "" });
        return ;
    }
    let result = await userService.loginByWorkId(workId, password);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 通过jwt检查用户登录状态接口
 */
router.post('/checkLoginByJWT', async function (req, res, next) {
    if (!req.body.jwt) {
        res.send({ msg: "令牌为空", login_status: false });
    } else {
        let result = await userService.checkLoginByJWT(req.body.jwt);
        res.json(result);
    }
});

/*
 * 新增用户接口
 * 需要权限限制
 */
router.post('/addUser', async function (req, res, next) {
    if (!req.headers.authorization) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    } else {
        // let accountName = req.body.accountName;
        let accountName = req.body.workId;  // 账号字段不需要了，以后使用工号和密码登录
        let username = req.body.username;
        let workId = req.body.workId;
        let facultyId = req.body.facultyId; // 学院ID，需要标注每个用户属于哪个学院的
        let phone = req.body.phone;
        let password = req.body.password;
        let email = req.body.email;
        let userType = req.body.userType;
        let allowLogin = req.body.allowLogin;
        // let createTime = req.formatTime;  服务器当前时间由于时区问题，时间对不上
        let createTime = req.body.createTime;
        if (!username || !workId || !facultyId || !phone || !password || !email || !userType || !allowLogin) {
            res.status(400).json({ success: false, msg: "参数不完整" });
            return ;
        }
        let result = await userService.addUser(req.headers.authorization, { accountName, username, workId, facultyId, phone, password, email, userType, allowLogin, createTime });
        res.status(result.success ? 200 : 400).json(result);
    }
});

/*
 * 获取所有用户列表接口
 */
router.get('/getAllUserList', async function (req, res, next) {
    if (!req.headers.authorization) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    let result = await userService.getAllUserList(req.headers.authorization);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取用户权限列表
 */
router.get('/getUserPowerList', async function (req, res, next) {
    if (!req.headers.authorization) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    let result = await userService.getUserPowerList(req.headers.authorization);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 新增角色接口: 角色 == 用户类型
 * 管理员、辅导员、学院书记 这些都是角色
 */
router.post('/addUserType', async function (req, res, next) {
    // 检测是否携带登录令牌
    let jwt = req.headers.authorization;
    if (!jwt) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    // 调用业务层进行验证登录验证权限以及执行真正的功能
    let userTypeName = req.body.userTypeName;
    let userTypeDesc = req.body.description;
    let userTypeBindingPower = req.body.userTypeBindingPower;
    if (!userTypeName || !userTypeDesc || !userTypeBindingPower) {
        res.status(400).json({ success: false, msg: "参数不完整" });
        return ;
    }
    let addUserTypeInfo = {
        userTypeName,
        userTypeDesc,
        userTypeBindingPower
    };
    let result = await userService.addUserType(jwt, addUserTypeInfo);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询所有用户类型的接口
 */
router.get('/getAllUserTypeList', async function (req, res, next) {
    // 检测是否携带登录令牌
    let jwt = req.headers.authorization;
    if (!jwt) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    let result = await userService.getAllUserTypeList(jwt);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询所有辅导员的 uid, workId, counselorId(辅导员ID) 的接口
 */
router.get('/getAllTeacherList', async function (req, res, next) {
    let jwt = req.headers.authorization;
    if (!jwt) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    let result = await userService.getAllTeacherList(jwt);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 修改用户信息
 */
router.post('/changeUserInfo', async function (req, res, next) {
    let jwt = req.headers.authorization;
    if (!jwt) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    let uid = req.body.uid;
    let username = req.body.username;
    let accountName = req.body.accountName;
    let password = req.body.password;
    let userTypeId = req.body.userTypeId;
    let phone = req.body.phone;
    let email = req.body.email;
    let facultyId = req.body.facultyId;
    if (!uid || !username || !accountName || !phone || !email || !facultyId) {
        res.status(400).json({ success: false, msg: "参数不完整" });
        return ;
    }
    let serviceFunc = userTypeId ? userService.changeUserInfoAndUserType : userService.changeUserInfo;
    let result = await serviceFunc(jwt, uid, username, accountName, phone, email, facultyId, password, userTypeId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 冻结用户
 */
router.post('/updateUserAllowLogin', async function (req, res, next) {
    let jwt = req.headers.authorization;
    let uid = req.body.uid;
    let allowLogin = req.body.allowLogin;
    if (!jwt) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    let result = await userService.updateUserAllowLogin(jwt, uid, allowLogin);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取某个用户类型的权限列表
 */
router.post('/getUserTypePowerList', async function (req, res, next) {
    let jwt = req.headers.authorization;
    let userType = req.body.userType;
    if (!jwt) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    if (!userType) {
        res.status(400).json({ success: false, msg: "参数不完整" });
        return ;
    }
    let result = await userService.getUserTypePowerList(jwt, userType);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 修改某个用户类型的权限列表
 */
router.post('/updateUserTypePowerList', async function (req, res, next) {
    let jwt = req.headers.authorization;
    let userType = req.body.userTypeId;
    let powerList = req.body.powerList;
    if (!jwt) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    if (!(Array.isArray(powerList)) || userType.length == 0) {
        res.status(400).json({ success: false, msg: "参数不完整，修改失败" });
        return ;
    }
    let result = await userService.updateUserTypePowerList(jwt, userType, powerList);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 根据用户名获取到 uid 和 workId
 */
router.post('/getUidAndWorkIdByUsername', async function (req, res, next) {
    let jwt = req.headers.authorization;
    let username = req.body.username;
    if (!jwt) {
        res.status(400).json({ success: false, msg: "令牌为空" });
        return ;
    }
    if (!username) {
        res.status(400).json({ success: false, msg: "参数不完整" });
        return ;
    }
    let result = await userService.getUidAndWorkIdByUsername(jwt, username);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;
